package com.view.inventtorymanage;

import com.bean.Customer;
import com.bean.Stock;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTxt;
import com.service.CustomerService;
import com.service.StockService;
import com.service.impl.CustomerServiceImpl;
import com.service.impl.StockServiceImpl;
import com.view.MainFrame;
import com.view.resourcemanage.CustomerFrame.CustomerUpdateDialog;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StcokUpdateDialog extends JDialog {

    private LovoButton addBtn;
    private LovoButton cancelBtn;
    private JLabel nameTxt,emailTxt,addressTxt,directorTxt,telTxt;
    private LovoTxt rangeTxt;


    private MainFrame frame ;
    private int key;
    private StockService stockService = new StockServiceImpl();

    public StcokUpdateDialog(MainFrame frame,int key) {
        // TODO Auto-generated constructor stub
        super(frame,"修改销售价",true);
        this.frame = frame;
        this.key = key;
        this.setBounds(350, 250, 520, 400);
        this.setLayout(null);
        this.init();
        this.setVisible(true);

    }



    private void init() {
        // TODO Auto-generated method stub
        Stock stock = new Stock();
        stock = stockService.showStock(key);

        nameTxt = new JLabel();
        nameTxt.setBounds(20,20,100,60);
        nameTxt.setText("<html>商品名："+stock.getGoods().getName()+" </html>");
        this.add(nameTxt);

        emailTxt = new JLabel();
        emailTxt.setBounds(20,90,100,60);
        emailTxt.setText("<html>厂家："+stock.getGoods().getManufactor()+" </html>");
        this.add(emailTxt);

        addressTxt = new JLabel();
        addressTxt.setBounds(20,160,100,60);
        addressTxt.setText("<html>类别："+stock.getGoods().getType()+" </html>");
        this.add(addressTxt);


        directorTxt = new JLabel();
        directorTxt.setBounds(300,20,150,60);
        directorTxt.setText("<html>建议采购价："+stock.getGoods().getBuyPrice()+" </html>");
        this.add(directorTxt);


        telTxt = new JLabel();
        telTxt.setBounds(300,90,150,60);
        telTxt.setText("<html>数量："+stock.getNumber()+" </html>");
        this.add(telTxt);


        rangeTxt = new LovoTxt("建议销售价",200,180,this);


        this.addBtn = new LovoButton("修改", 120, 320, this);
        this.cancelBtn = new LovoButton("取消", 270, 320, this);

        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                StcokUpdateDialog.this.dispose();;
            }
        });

        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int rangeTxtText =Integer.parseInt(rangeTxt.getText());

                if(stockService.updateSellPrice(key,rangeTxtText)){
                    JOptionPane.showMessageDialog(null,"修改成功");
                    StcokUpdateDialog.this.dispose();
                    frame.flush();
                }else {
                    JOptionPane.showMessageDialog(null,"修改失败");
                }
            }
        });

    }


}
